package flc.ast.adapter;


import static android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;

import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ConvertUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.stark.picselect.entity.SelectMediaEntity;

import flc.ast.R;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;
import stark.common.basic.utils.TimeUtil;

public class SelVideoAdapter1 extends StkProviderMultiAdapter<SelectMediaEntity> {

    public boolean hasQuery;
    public String keyWord;

    public SelVideoAdapter1() {
        addItemProvider(new StkSingleSpanProvider(126));
        addItemProvider(new SelVideoProvider());
    }

    private class SelVideoProvider extends BaseItemProvider<SelectMediaEntity> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_sel_video1;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, SelectMediaEntity selectMediaEntity) {
            Glide.with(getContext()).load(selectMediaEntity.getPath())
                    .into((ImageView) baseViewHolder.getView(R.id.ivSelVideoItemImg));
            baseViewHolder.setText(R.id.tvSelVideoItemName, selectMediaEntity.getMediaName());
            baseViewHolder.setText(R.id.tvSelVideoItemSize, ConvertUtils.byte2FitMemorySize(selectMediaEntity.getSize(), 0));
            baseViewHolder.setText(R.id.tvSelVideoItemDuration, TimeUtil.getMmss(selectMediaEntity.getDuration()));
            if (hasQuery) {
                baseViewHolder.setText(R.id.tvSelVideoItemName, matcherSearchText(Color.parseColor("#0D4EFB"), selectMediaEntity.getMediaName(), keyWord));
            }
        }
    }

    public static CharSequence matcherSearchText(int color, String string, String keyWord) {
        SpannableStringBuilder builder = new SpannableStringBuilder(string);
        int indexOf = string.indexOf(keyWord);
        if (indexOf != -1) {
            builder.setSpan(new ForegroundColorSpan(color), indexOf, indexOf + keyWord.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return builder;
    }

}
